﻿using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace ShutDownPc
{
    public partial class FrmMain : Form
    {
        private readonly Timer _timer = new Timer();

        public FrmMain()
        {
            InitializeComponent();
            btnStop.Enabled = false;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            StartTimer();
        }

        private void StartTimer()
        {
            var expireDate = DateTime.Parse($"{dtpShutDownDate.Value:yyyy-MM-dd} {dtpShutDownTime.Value:HH:mm:ss}");
            _timer.Interval = 10 * 1000;
            _timer.Tick += (sender, e) =>
            {
                if (DateTime.Now > expireDate)
                {
                    Process.Start("shutdown.exe", "-s -t 10");
                }
            };
            _timer.Start();

            SetControlStatus(false);
        }

        private void SetControlStatus(bool status)
        {
            btnStart.Enabled = status;
            dtpShutDownDate.Enabled = status;
            dtpShutDownTime.Enabled = status;
            btnStop.Enabled = !status;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            _timer.Stop();
            SetControlStatus(true);
        }
    }
}